#!/usr/bin/env python
# -*-coding: utf8 -*-
'''
# created by: iori
# created on: Tue 03 Dec 2013 03:55:32 PM CST
'''

import unittest
import sys, os, logging
logger = logging.getLogger(__name__)
sys.path.append('..')
from datetime import datetime
import threadpool
from voagrab import voa51_links, voa51_extract, down_mp3

baidu_ok = True
try:
    from baidu import get_pcs
except ImportError:
    baidu_ok = False

def voa51_backup(url='http://www.51voa.com/VOA_Standard_English/', days=1, storage='baidu', path='.', mthread=True):
    links = voa51_links(url, days)
    now = datetime.now()
    root = 'voa51/{0}/{1}/{{name}}.{{ext}}'.format(now.year, now.month).format
    if storage=='baidu' and baidu_ok:

        pcs = get_pcs()
        print pcs.quota
        for link in links:
            info = voa51_extract(link)
            pcs.save(info['url'], root(name=info['name'], ext='mp3'))
            pcs.save(info['content'], root(name=info['name'], ext='txt'))
    elif storage == 'local':
        pool = threadpool.ThreadPool(10)
        thread_args = []
        thread_records = []
        for link in links:
            info = voa51_extract(link)
            txt = os.path.join(path, root(name=info['name'], ext='txt'))
            mp3 = os.path.join(path, root(name=info['name'], ext='mp3'))
            if not os.path.isfile(txt):
                par = os.path.split(txt)[0]
                if not os.path.isdir(par):
                    os.makedirs(par)
                logger.info('download {}'.format(txt))
                with open(txt, 'wb') as fp:
                    fp.write(info['content'].encode('utf8'))
            if not os.path.isfile(mp3):
                if mthread:
                    thread_args.append(((info['url'], info['referer'], mp3), {}))
                    thread_records.append(mp3)
                else:
                    down_mp3(info['url'], info['referer'], mp3)
        if mthread:
            for i, req in enumerate(threadpool.makeRequests(down_mp3, thread_args)):
                logger.info(thread_records[i])
                pool.putRequest(req)
            pool.wait()


class TestIt(unittest.TestCase):
    def test_voa51_backup(self):
        voa51_backup(storage='local', path='res')
if __name__ == '__main__':
    unittest.main()
